package com.myx.zookeeper.zkclient.znode;

import com.myx.zookeeper.zkclient.ZookeeperConstant;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.serialize.SerializableSerializer;

import java.util.List;

/**
 * 描述:ZKClient 增删改查
 * @author: myx
 * @date: 2018/11/23
 * Copyright © 2018-hotpot. All rights reserved.
 */
public class ZKClientCrud<T>  {
    ZkClient zkClient;

    public ZKClientCrud() {
        this.zkClient = new ZkClient(ZookeeperConstant.connectString,5000,5000,new SerializableSerializer());
    }
    /***
     * 创建节点
     * @param path
     * @param data
     */
    public void createPersistent(String path,Object data){
        zkClient.createPersistent(path,data);
    }
    /***
     * 支持创建递归方式
     * @param path
     * @param createParents
     */
    public void createPersistent(String path,boolean createParents){
        zkClient.createPersistent(path,createParents);
    }

    /**
     * 读取数据
     * @param path
     * @return
     */
    public T readData(String path){
        return zkClient.readData(path);
    }

    /**
     * 获取子节点
     * @param path
     * @return
     */
    public List<String> getChildren(String path){
        return zkClient.getChildren(path);
    }

    /**
     * 赋值
     * @param path
     * @param object
     */
    public  void writeData(String path,Object object){
        zkClient.writeData(path,object);
    }

    /**
     * 删除
     * @param path
     */
    public  void deleteRecursive(String path){
        zkClient.deleteRecursive(path);
    }
}
